﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Jw.Models;
using Jw.Core;
using Jw.Web.Areas.Dev.Common;
using System.Web.Http;
using Newtonsoft.Json.Linq;

namespace Jw.Web.Areas.Dev.Controllers
{
    public class DjjhController : Controller
    {
        //
        // GET: /Dev/Djjh/

        public ActionResult Index()
        {
            var codeService = new sys_codeService();
            var model = new
            {
                idField = "Djjh_BillNo", //主键
                urls = DevHelper.GetIndexUrls("Djjh"),
                resx = DevHelper.GetIndexResx("点检计划"),
                dataSource = new
                {
                    yearItems = codeService.GetYearItems(),
                    monthItems = codeService.GetMonthItems()
                },
                form = new
                {
                    Djjh_BillNo = "",
                    Djjh_Sbbh = "",
                    Djjh_Bmbh = ""
                }
            };
            return View(model);
        }


        public ActionResult Edit(string id = "")
        {
            var data = new DjjhApiController().GetEditMaster(id);
            var codeService = new sys_codeService();

            var model = new
            {
                form = data.form,
                scrollKeys = data.scrollKeys,
                urls = DevHelper.GetEditUrls("Djjh"),
                resx = DevHelper.GetEditResx("点检计划"),
                dataSource = new
                {
                    // iswx = result
                },
                defaultForm = new b_Djjl().Extend(new
                {
                    Djjh_BillNo = id,
                    Djjh_Ywrq = DateTime.Now.ToShortDateString(),
                    Djjh_Kssj = DateTime.Now.ToShortDateString(),
                    Djjh_Jssj = DateTime.Now.ToShortDateString(),
                    Djjh_Zjdjrq = DateTime.Now.AddDays(-30).ToShortDateString(),
                    Djjh_Xcdjrq = DateTime.Now.AddDays(30).ToShortDateString(),
                    Djjh_Jhfy = 0,
                    Djjh_Djzq = 30,
                    Djjh_Jhrs = 1,
                    Djjh_Sbbh = "",
                    Djjh_Bmbh = ""
                }),
                defaultRow = new
                {

                },
                setting = new
                {
                    postFormKeys = new string[] { "Djjh_BillNo" },
                    postListFields = new string[] { "Djjhmx_BillNo", "Djjhmx_Flbh", "Djjhmx_Djxm", "Djjhmx_Jtyq", "Djjhmx_Fzry", "Djjhmx_Jcry", "Djjhmx_Remark" }
                }
            };
            return View(model);
        }

    }


    public class DjjhApiController : ApiController
    {
        public dynamic Get(RequestWrapper query)
        {
            query.LoadSettingXmlString(@"
            <settings defaultOrderBy='Djjh_BillNo'>
                <select>
                   a.*,b.Sbda_Sbmc
                </select>
                <from>
                   b_Djjh a Left Join b_Sbda b on b.Sbda_Sbbm = a.Djjh_Sbbh
                </from>
                <where defaultForAll='true' defaultCp='equal' defaultIgnoreEmpty='true' >
                    <field name='Djjh_BillNo' cp='like'></field>
                    <field name='A.ApprovePerson' cp='like'></field>
                    <field name='Djjh_Bzry' cp='like'></field>
                    <field name='Djjh_Ywrq' cp='daterange'></field>
                </where>
            </settings>
            ");
            var pQuery = query.ToParamQuery();
            var list = new b_DjjhService().GetDynamicListWithPaging(pQuery);
            return list;

        }
        public string GetNewBillNo()
        {
            return new b_DjjhService().GetNewKey("Djjh_BillNo", "dateplus");
        }
        public string GetNewRowId(int id, string BillNo)
        {
            return new b_DjjhmxService().GetNewKey("Djjhmx_Flbh", "maxplus", id, ParamQuery.Instance().AndWhere("Djjhmx_BillNo", BillNo));
        }


        public dynamic GetEditMaster(string id)
        {
            b_Djjh obj = new b_DjjhService().GetModel(ParamQuery.Instance().AndWhere("Djjh_BillNo", id));
            return new
            {
                form = obj,
                scrollKeys = new b_DjjhService().ScrollKeys("Djjh_BillNo", id.ToString())
            };
        }

        public dynamic GetDetail(string id)
        {
            var query = RequestWrapper
               .InstanceFromRequest()
               .SetRequestData("Djjhmx_BillNo", id)
               .LoadSettingXmlString(@"
            <settings defaultOrderBy='Djjhmx_Flbh'>
                <select>
                    A.*
                </select>
                <from>
                    b_Djjhmx A
                </from>
                <where>
                    <field name='Djjhmx_BillNo' cp='equal'></field>
                </where>
            </settings>");

            var pQuery = query.ToParamQuery();
            var result = new b_ByjlmxService().GetDynamicListWithPaging(pQuery);
            return result;

        }


        public List<dynamic> GetBillNo(string q)
        {
            var pQuery = ParamQuery.Instance().Select("top 10 Djjh_BillNo").AndWhere("Djjh_BillNo", q, Cp.StartWith);
            return new b_DjjhService().GetDynamicList(pQuery);
        }

        public List<dynamic> GetDoNames(string q)
        {
            var pQuery = ParamQuery.Instance().Select("distinct top 10 Djjh_Bzry").AndWhere("Djjh_Bzry", q, Cp.StartWithPY);
            return new b_DjjhService().GetDynamicList(pQuery);
        }

        public List<dynamic> GetApNames(string q)
        {
            var pQuery = ParamQuery.Instance().Select("distinct top 10 ApprovePerson").AndWhere("ApprovePerson", q, Cp.StartWithPY);
            return new b_DjjhService().GetDynamicList(pQuery);
        }

        public void Edit(dynamic data)
        {
            //   <columns ignore = 'Xhjh_BillNo'></columns>
            var formWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
            <settings>
                <table>{0}</table>
                <where><field name='Djjh_BillNo' cp='equal'></field></where>
            </settings>", typeof(b_Djjh).Name);

            var listWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
            <settings>
                <table>{0}</table>
                <where>
                    <field name='Djjhmx_BillNo' cp='equal'></field>
                    <field name='Djjhmx_Flbh'  cp='equal'></field>
                </where>

            </settings>", typeof(b_Djjhmx).Name);

            var result = new b_DjjhService().Edit(formWrapper, listWrapper, data);
        }

        public void Delete(string id)
        {
            var result = new b_DjjhService().Delete(ParamDelete.Instance().AndWhere("Djjh_BillNo", id));
            if (result <= 0)
            {
                DevHelper.ThrowHttpExceptionWhen(result <= 0, "单据删除失败[Djjh_BillNo={0}]，请重试或联系管理员！", id);
            }
            else
            {
                new b_ByjhmxService().Delete(ParamDelete.Instance().AndWhere("Djjhmx_BillNo", id));
            }
        }

        //审核
        [System.Web.Http.HttpPost]
        public void Audit(string id, JObject data)
        {
            var status = data["status"].ToString();
            var comment = data["comment"].ToString();
            var result = new DevService().Audit(typeof(b_Djjh).Name, "Djjh_BillNo", id, status, comment);
            DevHelper.ThrowHttpExceptionWhen(result < 0, "单据审核失败[Djjh_BillNo={0}]，请重试或联系管理员！", id);
        }


    }
}
